<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
    <el-form-item label="补工单号" prop="mendWorkNumber	">
        <el-input
          v-model="queryParams.mendWorkNumber	"
          placeholder="请输入补工单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="员工名称" prop="staffName">
        <el-input
          v-model="queryParams.staffName"
          placeholder="请输入员工名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="工时类型" prop="manHourType">
        <el-select
          v-model="queryParams.manHourType"
          placeholder="请选择工时类型"
          clearable
          class="!w-220px"
          filterable
          remote
          reserve-keyword
          :remote-method="getManHourTypeList"
        >
        <el-option
            v-for="item in manHourTypeOptions"
            :key="item.id"
            :label="item.manHourType"
            :value="item.manHourType"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="dictMendWorkMonadStatus">
        <el-select
          v-model="queryParams.dictMendWorkMonadStatus"
          placeholder="请选择状态"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="dict in getIntDictOptions('mend_work_monad_status')"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务下达时间" prop="taskIssuedDate">
        <el-date-picker
          v-model="queryParams.taskIssuedDate"
          value-format="YYYY-MM-DD"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" type="primary"> 搜索</el-button>
        <el-button @click="resetQuery"> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
  <div class="btnWrap">
      <div>
        <el-button
          type="primary"
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['salary:accounting-mend-work-monad:export']"
        >
          数据导出
        </el-button>
        <el-button
          type="primary"
          @click="openForm('create')"
          v-hasPermi="['salary:accounting-mend-work-monad:create']"
        >
          新增
        </el-button>
      </div>
      <div>
        <el-button type="primary" @click="openTableTitleSet"> 表头设置 </el-button>
      </div>
    </div>
  <Table
      :columns="tableTitleList"
      :selection="tableObject.tableList.length?true:false"
      @register="tableoperation"
      @select="selectionChange"
      @update:currentPage="getTableData"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      :tableHeight="tableHeight"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      >
      <template #action="{ row }">
        <cell>
          <el-button 
          link type="primary"  
          @click="openForm('update',row.id)"
          v-if="row.dictMendWorkMonadStatus !=50"
          >编辑</el-button> 
        </cell>
        <cell>
          <el-button link type="primary"  @click="openForm('view',row.id)" v-hasPermi="['salary:accounting-mend-work-monad:view']">查看</el-button> 
        </cell>
      </template>
    </Table>
  </ContentWrap>

  <!-- 上传文件 -->
  <CusUploadFile ref="cusUploadFile" @refresh="getList" />
  <!-- 导出 -->
  <ExportFile ref="exportRef" @download="downloadFun" />
  <!-- 表头设置 -->
  <table-width-set ref="tableTitleSet" @refresh="getLabelAll" />
</template>

<script setup lang="ts" name="AccountingMendWorkMonad">
import { getIntDictOptions } from '@/utils/dict'
import download from '@/utils/download'
import * as AccountingMendWorkMonadApi from '@/api/payroll/shopWagesCalculate/accountingMendWorkMonad'
import * as WageManHourValuationApi from '@/api/config/wageManHourValuation'
// const message = useMessage() // 消息弹窗
// const { t } = useI18n() // 国际化
const loading = ref(true) // 列表的加载中
// const total = ref(0) // 列表的总页数
// const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  staffName: null,
  manHourType: null,
  dictMendWorkMonadStatus: null,
  mendWorkNumber: null
})
const queryFormRef = ref() // 搜索的表单
let tableTitleList = reactive<CrudSchema[]>([]) // 表头的数据
const { push } = useRouter()
const exportLoading = ref(false) // 导出的加载中
const { tableObject, tableMethods } = useTable({
    getListApi: AccountingMendWorkMonadApi.getAccountingMendWorkMonadPage, // 分页接口
    defaultParams: queryParams
  })
  // 获得表格的各种操作
const { getList, setSearchParams } = tableMethods

/** 查询列表 */
const getLabelAll = async () => {
    loading.value = true
    try {
      const data = await AccountingMendWorkMonadApi.getAccountingMendWorkMonadPage(queryParams)
      const { allSchemas } = useCrudSchemas(data.labels)
      allSchemas.tableColumns.push({
        align: 'left',
        field: 'action',
        label: '操作',
        width: 110
      })
      tableTitleList = allSchemas.tableColumns
      getList()
    } finally {
      loading.value = false
    }
  }

  /** 查询列表 */
  const getTableData = async (val) => {
    tableObject.currentPage = val
    getList()
  }

 /**表头内部方法 */
const usTableoperation = ref()
const tableoperation = (table) => {
  usTableoperation.value = table
}

// 清空多选
const clearSelectData = ()=>{
  usTableoperation.value.selections = []
  usTableoperation.value.clearTableSelect()
}

  /** 搜索按钮操作 */
  const handleQuery = () => {
      queryParams.pageNo = 1
      // if(registerData.value != null){
      //   queryParams.registerDataRadius = registerData.value
      // }
    getList()
  }

  /** 重置按钮操作 */
  const resetQuery = () => {
    queryFormRef.value.resetFields()
    registerData.value = null
    queryParams.registerDataRadius=null
    setSearchParams(queryParams)
  }

  /** 编辑/查看操作 */
  const openForm = (type: string, id?: number) => {
    console.log(type,'type')
    push('/payroll/payroll/shopWagesCalculate/AccountingMendWorkMonadForm?type=' + type + '&id=' + id)
  }

  /** 导出按钮操作 */
  const exportRef = ref()
  const handleExport = () => {
    exportRef.value.open()
  }

  //导出数据
  const downloadFun = async () => {
  const val = exportRef.value.params
  let params = {
      fileName: val.name,
      ids: ''
    }
    if (val.range === 'all') {
      params.ids = []
    } else {
      params.ids = usTableoperation.value.selections.map( item => item.id)
    }
    const data = await AccountingMendWorkMonadApi.exportAccountingMendWorkMonad(params)
    download.excel(data, val.name)
    clearSelectData()
  }


    /**打开表格表头设置**/
  const tableTitleSet = ref()
  const openTableTitleSet = () => {
    const arr = tableTitleList.slice(0, -1)
    let code = 'AccountingMendWorkMonad'
    tableTitleSet.value.open(arr, code)
  }

  //获取工时类型
  const manHourTypeOptions = ref()
  const getManHourTypeList = async (query) =>{
  let res = await WageManHourValuationApi.getWageManHourValuationPage({
    pageNo: 1,
    pageSize: 10,
    dictManHourValuationStatus: 10,
    manHourType:query
  })
  manHourTypeOptions.value = res.list
}
  /** 初始化 **/
  const tableHeight = ref(0)
  onMounted(async () => {
    const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
    tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 127
    await getLabelAll()
    getList()
  })

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
<style lang="scss" scoped>
  .btnWrap {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }
</style>
